﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author YUga studio
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.service;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.entity.Supplier;
import com.oplink.webbase.modules.invoicing.dao.SupplierDao;



/**
 * 告警Service
 * @author YUga studio
 * @version 2015-01-12
 */
@Component
@Transactional
public class SupplierService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SupplierService.class);
	
	@Autowired
	private SupplierDao supplierDao;
	
	public Supplier get(Long id) {
		return supplierDao.findOne(id);
	}
	
	public Page<Supplier> find(Page<Supplier> page, Supplier supplier) {
		DetachedCriteria dc = supplierDao.createDetachedCriteria();
		//添加查询条件
		if (null != supplier.getSupplierId()
				&& StringUtils.isNotEmpty(supplier.getSupplierId().toString())) {
			dc.add(Restrictions.like("supplierId", "%"+supplier.getSupplierId()+"%"));
		}
        if (StringUtils.isNotEmpty(supplier.getSupplierName())){
			dc.add(Restrictions.like("supplierName", "%"+supplier.getSupplierName()+"%"));
		}
        if (StringUtils.isNotEmpty(supplier.getPhone())){
			dc.add(Restrictions.like("phone", "%"+supplier.getPhone()+"%"));
		}
		dc.add(Restrictions.eq(Supplier.DEL_FLAG, Supplier.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return supplierDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<Supplier> getUserSupplier(Page<Supplier> page, Supplier supplier) {
		DetachedCriteria dc = supplierDao.createDetachedCriteria();
		//添加查询条件
		if (null != supplier.getSupplierId()
				&& StringUtils.isNotEmpty(supplier.getSupplierId().toString())) {
			dc.add(Restrictions.like("supplierId", "%"+supplier.getSupplierId()+"%"));
		}
        if (StringUtils.isNotEmpty(supplier.getSupplierName())){
			dc.add(Restrictions.like("supplierName", "%"+supplier.getSupplierName()+"%"));
		}
        if (StringUtils.isNotEmpty(supplier.getPhone())){
			dc.add(Restrictions.like("phone", "%"+supplier.getPhone()+"%"));
		}
		dc.add(Restrictions.eq(Supplier.DEL_FLAG, Supplier.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return supplierDao.find(page, dc);
	}
	
	public Supplier getSupplier(Long id) {
		return supplierDao.findOne(id);
	}

	public void saveSupplier(Supplier entity) {
		supplierDao.save(entity);
	}
	
	public void deleteSupplier(Long id) {
		supplierDao.delete(id);
	}
	
	@Autowired
	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}	
}
